package com.ruibang.glass.equipment.service.impl.device;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.common.domain.file.FileBusiness;
import com.ruibang.glass.equipment.domain.req.device.DeviceAllPageReq;
import com.ruibang.glass.equipment.entity.device.AccidentRectification;
import com.ruibang.glass.equipment.feign.FileApi;
import com.ruibang.glass.equipment.mapper.device.AccidentRectificationMapper;
import com.ruibang.glass.equipment.service.AccidentRectificationService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 事故台账服务类
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@Slf4j
@Service
public class AccidentRectificationServiceImpl extends ServiceImpl<AccidentRectificationMapper, AccidentRectification> implements AccidentRectificationService {

    @Autowired
    private FileApi fileApi;

    @Autowired
    private RedisSingleService redisService;


    /**
     * 事故台账
     *
     * @param deviceAllPageRea
     * @return
     */
    @Override
    public IPage<AccidentRectification> queryPage(DeviceAllPageReq deviceAllPageRea) {
        IPage<AccidentRectification> checkList = baseMapper.getCheckList(new Page<>(deviceAllPageRea.getPageNo(), deviceAllPageRea.getPageSize()), deviceAllPageRea);
        List<AccidentRectification> records = checkList.getRecords();
        List<AccidentRectification> newList = new ArrayList<>();
        for (AccidentRectification accidentRectification : records) {
            AccidentRectification byId = getById(accidentRectification.getId());
            newList.add(byId);
        }
        checkList.setRecords(newList);
        return checkList;
    }

    /**
     * 跟新
     *
     * @param accidentRegister
     * @return
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody updateOrAddById(AccidentRectification accidentRegister) {
        if (StringUtils.isNotBlank(accidentRegister.getResponsiblePerson())) {
            Map<Object, Object> userMap = redisService.hmget(RedisKey.USER_GROUP_TYPE);
            if (userMap != null) {
                accidentRegister.setResponsiblePerson(userMap.get(accidentRegister.getResponsiblePerson()) == null ? "" : userMap.get(accidentRegister.getResponsiblePerson()).toString());
                accidentRegister.setResponsiblePersonId(accidentRegister.getResponsiblePerson());
            }
        }
        //删除文件
        if (CollectionUtils.isNotEmpty(accidentRegister.getRemoveIdList())) {
            fileApi.deleteByFileIds(accidentRegister.getRemoveIdList());
        }
        //更新文件表数据
        if (CollectionUtils.isNotEmpty(accidentRegister.getFilesIds())) {
            FileBusiness fileBusiness = new FileBusiness();
            fileBusiness.setBusinessId(accidentRegister.getId());
            fileBusiness.setFileIds(accidentRegister.getFilesIds());
            fileApi.updateBusinessById(fileBusiness);
        }
        if (StringUtils.isBlank(accidentRegister.getId())) {
            baseMapper.insert(accidentRegister);
            return ResultBody.success();
        }
        baseMapper.updateById(accidentRegister);
        return ResultBody.success();
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public AccidentRectification getById(String id) {
        AccidentRectification accidentRectification = baseMapper.selectById(id);
        if (accidentRectification != null) {
            ResultBody<List<String>> resultBody = fileApi.getFileIdsByBusinessId(id);
            if (resultBody != null && CollectionUtils.isNotEmpty(resultBody.getData())) {
                accidentRectification.setFilesIds(resultBody.getData());
            }
        }
        return accidentRectification;
    }


}
